import { UserAvatar, Footer } from '@/components';
import { fetchDictData, fetchUserData } from '@/services/initial';
import { history } from '@umijs/max';
import { Typography, message } from 'antd';
const isDev = process.env.NODE_ENV === 'development';
const { Text } = Typography;
const loginPath = '/login';

//请求接口配置
export const request = {
  baseURL: '/prod-api',
  timeout: 10000,
  // other axios options you want
  errorConfig: {
    errorHandler() {},
    errorThrower() {},
  },
  requestInterceptors: [
    (url, options) => {
      //设置登陆token
      const token = localStorage.getItem('token');
      const headers = { Authorization: token };
      return {
        url,
        options: { ...options, headers },
      };
    },
  ],
  responseInterceptors: [
    // 直接写一个 function，作为拦截器
    (response) => {
      const { status, data } = response;
      if (status === 200) {
        const { code, msg } = data;
        if (code === 401) {
          history.push(loginPath);
        } else if (code === 500) {
          message.error(msg);
        }
      } else if (status === 401) {
        //未登录或登录失效
      }
      return response;
    },
  ],
};

export async function getInitialState(params) {
  //每次刷新页面，这个方法都会执行，首先会判断是否用户信息过期，过期跳转登录页面，如果未过期，将用户的数据储存到一个容器中，作为全局共享的数据
  const fetchUserInfo = async () => {
    try {
      //获取用户信息
      const userData = await fetchUserData();
      const currentUser = userData?.data;
      //获取字典表数据
      let dictData_ = await fetchDictData();
      dictData_ = dictData_?.data;
      let dictData = {};
      dictData_.forEach(({ sysDictData, dictName }) => {
        dictData[dictName.trim()] = sysDictData.map(
          ({ dictLabel, dictValue }) => {
            return { label: dictLabel, value: dictValue };
          },
        );
      });
      return {
        currentUser,
        dictData,
      };
    } catch (error) {
      history.push(loginPath);
    }
    return undefined;
  };
  // 如果不是登录页面，执行
  const { location } = history;
  if (location.pathname !== loginPath) {
    const data = await fetchUserInfo();
    return {
      ...data,
      fetchUserInfo,
    };
  }
  return {
    fetchUserInfo,
  };
}

//布局配置
export const layout = ({ initialState, setInitialState }) => {
  // 其他属性见：https://procomponents.ant.design/components/layout#prolayout
  return {
    title: '专利系统',
    logo: 'https://img.alicdn.com/tfs/TB1YHEpwUT1gK0jSZFhXXaAtVXa-28-27.svg',
    navTheme: 'light',
    colorPrimary: '#2b69ec',
    layout: 'top',
    contentWidth: 'Fluid',
    fixedHeader: true,
    menu: { locale: false },
    style: {
      minHeight: '98vh',
    },
    waterMarkProps: {
      content: initialState?.currentUser?.nickName,
    },
    rightRender: () => <UserAvatar userInfo={initialState?.currentUser} />,
    footerRender: () => <Footer />,
  };
};

//路由切换配置
export function onRouteChange(params) {
  const { location, clientRoutes, routes, action, basename, isFirst } = params;
}

//打平的路由列表
export function patchRoutes({ routes, routeComponents }) {}
